package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.product.dto.ProductParam;
import com.pig4cloud.mall.product.entity.Product;
import com.pig4cloud.mall.product.service.ProductService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商品信息
 *
 * @author fangyu
 * @date 2020-12-26 19:40:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/spu")
@Slf4j
@Api(value = "spu" , tags = "商品管理")
public class ProductController {

    private final  ProductService productService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param productParam 前端请求商品信息
     * @return
     */
    @ApiOperation(value = "分页查询" , notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_list_get')")
    public R getProductPage(Page page, ProductParam productParam) {
    	log.info("current: {}, size: {}", page.getCurrent(), page.getSize());
    	log.info("param: {}", productParam.getName());
        return R.ok(productService.list(page, productParam));
    }


    /**
     * 通过id查询商品信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询" , notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('product_product_get')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(productService.getById(id));
    }

    /**
     * 新增商品信息
     * @param product 商品信息
     * @return R
     */
    @ApiOperation(value = "新增商品信息" , notes = "新增商品信息")
    @SysLog("新增商品信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('product_product_add')")
    public R save(@RequestBody Product product) {
        return R.ok(productService.save(product));
    }

    /**
     * 修改商品信息
     * @param product 商品信息
     * @return R
     */
    @ApiOperation(value = "修改商品信息" , notes = "修改商品信息")
    @SysLog("修改商品信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('product_product_edit')")
    public R updateById(@RequestBody Product product) {
        return R.ok(productService.updateById(product));
    }

    /**
     * 通过id删除商品信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除商品信息" , notes = "通过id删除商品信息")
    @SysLog("通过id删除商品信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('product_product_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(productService.removeById(id));
    }

}
